package test

import (
	"os"
	"path/filepath"
)

// FindProjectRoot 从当前目录向上查找 go.mod 所在的目录，作为项目根目录
func FindProjectRoot() (string, error) {
	dir, err := os.Getwd()
	if err != nil {
		return "", err
	}
	for {
		if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
			return dir, nil
		}
		parent := filepath.Dir(dir)
		if parent == dir {
			// 已经到根目录了
			return "", os.ErrNotExist
		}
		dir = parent
	}
}

func LoadClusterConfig(clusterFileName string) ([]byte, error) {
	rootPath, err := FindProjectRoot()
	if err != nil {
		return nil, err
	}
	return os.ReadFile(filepath.Join(rootPath, "static", "test-data", "k8s-cluster-context", clusterFileName))
}
